package com.taijihuabao.common.commonutils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changhui.tu
 */
@Configuration
@EnableSwagger2
@SuppressWarnings("unchecked")
public class SwagerConfig {

    @Bean
    public Docket docket() {
        DocumentationType swagger2 = new DocumentationType("swagger", "2.0");
        //设置个人信息
        Contact defaultContact = new Contact("练习作业", "010-6666-8888", "work@qq.com");
        //设置文档信息
        ApiInfo apiInfo = new ApiInfo("用户角色权限管理系统", "实习作业接口文档", "1.0", "http://www.aaaTU/java/index", defaultContact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
        return new Docket(swagger2)
                //设置文档信息
                .apiInfo(apiInfo)
                //设置扫描包
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.taijihuabao.modules.controller"))
                .build()
                .globalOperationParameters(getParameterList());
    }

    /**
     * 添加head卷劫配置start
     * required(false) 表示userToken非必填，因为有的请求无需token验证。
     *
     * @return
     */
    @Bean
    public List<Parameter> getParameterList() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }

}

